<!-- 已失效-详情 -->
<template>
  <div class="index-container">
    <div class="banxing">
      <div class="imgs">
        <img :src="list.image" alt />
      </div>
      <div>
        <div class="banxing_title">{{list.name}}</div>
        <div class="banxing_sku">{{list.sku_name}}</div>
        <div class="grid_time">
          <span class="grid_shoujia">¥{{list.price}}</span>
          <span v-if="list.orig_price != '0.00'" class="grid_yuanjia">¥{{list.orig_price}}</span>
          <span class="grid_status">交易失效</span>
        </div>
      </div>
    </div>

    <div class="jiange"></div>

    <van-field v-model="list.sn" label="订单号：" />
    <van-field v-model="list.addtime" label="下单时间：" />
    <div class="jiange"></div>

    <van-field label="收货地址：" />
    <!-- <van-field v-model="name" />
    <van-field v-model="dizhi" />-->
    <van-field v-model="list.express_name + '        ' + list.express_phone" />
    <van-field
      v-model="list.express_province + list.express_city + list.express_town + list.express_addr"
    />

    <van-submit-bar
      label="应支付："
      :disabled="isDisabledSubmitBtn"
      :decimal-length="2"
      :price="parseInt(list.price)*100"
      button-text="重新购买"
      @submit="onSubmit"
      button-color="#009697"
    />
  </div>
</template>

<script>
import moment from 'moment'
// import { weixinPay } from '@/utils/weixinPay.js'
import { detailOrders } from '@/api/mine/order'
import { makeOrders } from '@/api/home/confirm-order'
export default {
  data() {
    return {
      isDisabledSubmitBtn: false,
      list: {},
      // checked: [],
      checkboxRefs: [],
      checked: '1',
      weChatParameter: {}
    }
  },

  computed: {},

  mounted() {
    this.fetchList()
  },

  methods: {
    // 获取个人信息
    fetchList() {
      const data = {
        sn: JSON.parse(decodeURIComponent(this.$route.query.row)).sn
      }
      detailOrders(data).then(res => {
        this.list = res.data
        this.list.addtime = moment(this.list.addtime * 1000).format('YYYY-MM-DD HH:mm:ss')
      })
    },
    toggle(index) {
      this.checkboxRefs.value[index].toggle()
    },
    onSubmit() {
      const data = {
        goods_id: this.list.goods_id,
        sku_id: this.list.sku_id || 0,
        client: 'android'
      }
      this.isDisabledSubmitBtn = true
      makeOrders(data).then(res => {
        this.weChatParameter = res.data.orderinfo
        this.weixinPay()
      }).catch(error => {
        this.isDisabledSubmitBtn = false
        this.$toast(error.msg)
      })
    },

    // h5方法调取微信支付
    weixinPay(params) {
      var that = this
      if (typeof WeixinJSBridge === 'undefined') {
        if (document.addEventListener) {
          document.addEventListener('WeixinJSBridgeReady', that.onBridgeReady(params), false)
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', that.onBridgeReady(params))
          document.attachEvent('onWeixinJSBridgeReady', that.onBridgeReady(params))
        }
      } else {
        that.onBridgeReady()
      }
    },
    // 微信内置浏览器类，weChatParameter对象中的参数是3.步骤代码中从后端获取的数据
    onBridgeReady() {
      var that = this
      // var timestamp = Math.round(that.weChatParameter.timeStamp).toString()
      WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
          debug: true,
          'appId': that.weChatParameter.appId, // 公众号名称，由商户传入
          'timeStamp': that.weChatParameter.timeStamp, // 时间戳，自1970年以来的秒数
          'nonceStr': that.weChatParameter.nonceStr, // 随机串
          'package': that.weChatParameter.package,
          'signType': that.weChatParameter.signType, // 微信签名方式：
          'paySign': that.weChatParameter.paySign, // 微信签名
          jsApiList: [
            'chooseWXPay'
          ]
        },
        function (res) {
          // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
          if (res.err_msg == 'get_brand_wcpay_request:ok') { // 支付成功后的操作
            that.$toast({
              message: '支付成功',
              duration: 2000
            })
            that.isDisabledSubmitBtn = true
          } else if (res.err_msg == 'get_brand_wcpay_request:cancel') { // 取消支付的操作
            that.$toast({
              message: '取消支付',
              duration: 2000
            })
            that.isDisabledSubmitBtn = false // 按钮恢复高亮
          } else { // 支付失败的操作
            that.$toast({
              message: '支付失败',
              duration: 2000
            })
            that.isDisabledSubmitBtn = false // 按钮恢复高亮
          }
        }
      )
    }
  }
}
</script>

<style lang="scss" scoped>
.jiange {
  height: 10px;
  background: #f7f7f7;
}

.dingdanhao {
  border-bottom: 1px solid #ddd;
  .yuyueren {
    float: right;
    margin-top: 6px;
    width: 82px;
    height: 25px;
    padding: 0;
    background: rgba(7, 120, 121, 0.1);
    font-size: 12px;
    color: #11a8a4;
  }
}

.banxing {
  padding: 15px;
  display: flex;
  border-bottom: 1px solid #f2f2f2;
  .imgs {
    margin-right: 9px;
    img {
      width: 112px;
      height: 75px;
      border-radius: 5px;
      overflow: hidden;
    }
  }
}

.banxing_title {
  // height: 40px;
  font-size: 14px;
  font-family: PingFang SC;
  font-weight: bold;
  color: #333333;
  line-height: 21px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
.banxing_sku {
  font-size: 14px;
  font-weight: 500;
  color: #333333;
}
.grid_time {
  margin-top: 10px;
  font-size: 12px;
  font-weight: 400;
  color: #999999;
  .grid_shoujia {
    margin-right: 5px;
    font-size: 16px;
    font-weight: bold;
    color: #009697;
    line-height: 30px;
  }
  .grid_yuanjia {
    font-size: 12px;
    font-weight: 500;
    text-decoration: line-through;
    color: #999999;
  }
  .grid_status {
    float: right;
    font-size: 14px;
    font-weight: 500;
    color: #fd953f;
  }
}

::v-deep .van-submit-bar {
  height: 70px;
  background: #ffffff;
  box-shadow: 0px -1px 4px 0px rgba(66, 66, 66, 0.19);
  .van-submit-bar__bar {
    height: 70px;
  }
  .van-submit-bar__price {
    color: #009697;
  }
  .van-button {
    width: 130px;
    height: 44px;
    line-height: 44px;
    background: #009697;
    border-radius: 22px;
    font-size: 16px;
  }
}
</style>

